<template>
  <div class="app-container">
    <el-form @submit.native.prevent :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px" :label-position="labelPosition">
      <el-form-item label="仓库" prop="spckmc">
        <el-select
            v-model="queryParams.spckid"
            placeholder="请选择"
            clearable
            @change="changeWarehouse"
            filterable
        >
            <el-option
                v-for="item in warehouseList"
                :key="item.spckid"
                :label="item.spckmc"
                :value="item.spckid"
            />
        </el-select>
      </el-form-item>
      <el-form-item label="所选日期" prop="warehouseOperationDate">
        <el-date-picker
          v-model="warehouseOperationDate"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :clearable="false"
          @change="warehouseOperationDateChange()">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div>
      <el-button size="mini" :type="pageNum == dateArr.length - index?'primary':''" plain v-for="(item, index) in dateArr" :key="index" class="mr10" @click="dateClick(dateArr.length - index)">{{item}}</el-button>
    </div>
    <el-table
      v-loading="loading"
      :data="warehouseOperationList"
      ref="warehouseOperationTable"
    > 
      <af-table-column label="操作" align="center" class-name="small-padding fixed-width" width="80" fixed="left">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="openWarehouseOperationDetail(scope.row)"
            v-hasPermi="['zhcloud:sjfx:list']"
          >详细</el-button>
        </template>
      </af-table-column>
      <af-table-column prop="ssrqid" label="日期" fixed="left"></af-table-column>
      <af-table-column prop="spckmc" label="仓库"></af-table-column>
      <af-table-column prop="spjsi" label="入仓件数"></af-table-column>
      <af-table-column prop="spsli" label="入仓重量(吨)"></af-table-column>
      <af-table-column prop="spjso" label="出仓件数"></af-table-column>
      <af-table-column prop="spslo" label="出仓重量(吨)"></af-table-column>
      <af-table-column prop="spsl" label="作业量"></af-table-column>
      <af-table-column prop="jlr" label="净流入量"></af-table-column>
      <af-table-column prop="hzl" label="货转量"></af-table-column>
      <af-table-column prop="num" label="出库单数量(份)"></af-table-column>
      <af-table-column prop="pjmdckzl" label="平均单量(吨)"></af-table-column>
    </el-table>
    <div class="mt20" v-show="warehouseOperationListLast" :style="{fontSize:'14px'}">
      <span>{{warehouseOperationListLast.spckmc}}</span>
      <span class="text-danger">{{warehouseOperationListLast.ssrqid}}</span>
      <span>出仓分析：</span>
      <span>{{warehouseOperationListLast.spjsi}}</span>
      <span class="text-danger">{{warehouseOperationListLast.spsli}}</span>
      <span>份; </span>
      <span>{{warehouseOperationListLast.spjso}}</span>
      <span class="text-danger">{{warehouseOperationListLast.spslo}}</span>
      <span>份; </span>
      <span>{{warehouseOperationListLast.spsl}}</span>
      <span class="text-danger">{{warehouseOperationListLast.jlr}}</span>
      <span>份; </span>
      <span>{{warehouseOperationListLast.hzl}}</span>
      <span class="text-danger">{{warehouseOperationListLast.num}}</span>
      <span>份</span>
    </div>
    <div id="warehouseOperationChartId" :style="chartStyle" ></div>
  </div>
</template>

<script>
import { getListWarehouseOperation, getWarehouseOperationChart } from "@/api/storage/dataAnalysis/warehouseOperation";
import { selectWarehouse17 } from "@/api/storage/warehouse"
import { getAllDates, formatDate } from '@/utils'
import echarts from 'echarts';
import variables from '@/assets/styles/variables.scss'

export default {
  name: "WarehouseOperation",
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      //对齐
      labelPosition: "right",
      //加载列表
      loading: true,
      //加载仓库
      selectWarehouse17Loading: true,
      warehouseOperationDate:'',
      // 查询参数
      queryParams: {
        ally: "",
        khid: "",
        khlx: "",
        khmcid: "",
        rqid: "",
        spckid: "",
        spkhmcid: "",
        ssrqid: "",
        ssrqid1: "",
        ssrqid2: "",
        zt: "按日",
      },
      //获取仓位参数
      selectPositionQuery:{
         spckid:""
      },
      //获取客户参数
      selectClient3Query: {
        name1:""
      },
      //前n天日期数组
      dateArr:[],
      //仓位列表
      warehousePositionList:[],
      //批量审核数据
      selection:[],
      // 仓库作业量列表
      warehouseOperationList: [],
      warehouseOperationListLast:"",
      //仓库列表
      warehouseList:[],
      //客户列表
      client3List: [],
      //图表属性
      chartStyle: {
        height:"600px",
        width:"1000px",
        margin:"60px auto"
      },
      //页数
      pageSize: 10,
      //页码
      pageNum:7,
      // 总条数
      total: 0,
    };
  },
  beforeRouteEnter(to, from, next) {
    const params = to.params
    next(vm => {   
      if(Object.keys(params).length > 0) {
        if(to.params.refresh == true) {
          vm.getList()
        }
      }
    })
  },
  created() {
    const date1 = new Date()
    let date2 = new Date(date1)
    date2.setDate(date1.getDate() - 6)
    let date3 = new Date(date1)
    date3.setDate(date1.getDate() - 6)
    const dates = getAllDates(date2, date1)
    this.dateArr = dates.reverse()
    const ssrqid1 = formatDate(date3).split(' ')[0]
    const ssrqid2 = formatDate(date1).split(' ')[0]

    this.queryParams.ssrqid1 = ssrqid1
    this.queryParams.ssrqid2 = ssrqid2
    this.warehouseOperationDate = [ssrqid1, ssrqid2]
    this.getWarehouse()
    this.getList()
  },
  mounted() {
    this.getChart()
  },
  watch: {
    'warehouseOperationDate': function(data) {
      if(!data) return
      const date1 = new Date(data[0])
      const date2 = new Date(data[1])
      const dates = getAllDates(date1, date2)
      this.dateArr = dates.reverse()
      this.pageNum = this.dateArr.length
    }
  },
  methods: {
    /**获取仓库 */
    getWarehouse() {
      this.selectWarehouse17Loading = true
      selectWarehouse17({}).then(response => {
        this.warehouseList = response.data
        this.selectWarehouse17Loading = false
      });
    },
    /** 查询仓库作业量图表信息 */
    getChart() {
      let chart = echarts.init(document.getElementById('warehouseOperationChartId'))
      chart.showLoading({
        text:"",
        color:"#192C59",
        textCloor:"#192C59",
        spinnerRadius: 18,
        lineWidth: 2,
      })
      getWarehouseOperationChart(this.queryParams).then(response => {
        const data = response.data
        chart.hideLoading()
        this.initChart(data, chart)
      });
    },

    /** 查询仓库作业量图表 */
    getList() {
      this.loading = true
      getListWarehouseOperation(this.queryParams, {pageNum:this.pageNum, pageSize:this.pageSize}).then(response => {
        const data = response.data
        this.loading = false
        if(data.length == 0) {
          this.warehouseOperationList = data
          this.warehouseOperationListLast = ""
        }
        if(data.length > 0) {
          const lastData = data[data.length - 1]
          if(lastData.spjsi == "2吨以下(份):" && lastData.spckmc == "中恒塱宝仓") {
            const data1 = data.splice(0, data.length - 1)
            const data2 = data.splice(data.length - 1, data.length)
            this.warehouseOperationList = data1
            const data2Obj = data2[0]
            data2Obj.spjsi = data2Obj.spjsi.slice(0,data2Obj.spjsi.length - 4)
            data2Obj.spjso = data2Obj.spjso.slice(0,data2Obj.spjso.length - 4)
            data2Obj.spsl = data2Obj.spsl.slice(0,data2Obj.spsl.length - 4)
            data2Obj.hzl = data2Obj.hzl.slice(0,data2Obj.hzl.length - 4)
            this.warehouseOperationListLast = data2Obj
          } else {
            this.warehouseOperationList = data
            this.warehouseOperationListLast = ""
          }
        }
      });
    },

    /**选择仓库 */
    changeWarehouse() {
      this.getList()
      this.getChart()
    },

    /**选择日期*/
    warehouseOperationDateChange() {
      this.queryParams.ssrqid1 = this.warehouseOperationDate?this.warehouseOperationDate[0]:""
      this.queryParams.ssrqid2 = this.warehouseOperationDate?this.warehouseOperationDate[1]:""
      this.getList()
      this.getChart()
    },

    /**打开详情 */
    openWarehouseOperationDetail(data) {
      this.$router.push({path:"/storage/dataAnalysis/warehouseOperation/warehouseOperationDetail", query:{
        spckid:data.spckid,
        zt:data.zt,
        ssrqid:data.ssrqid,
        spckmc:data.spckmc
      }})
    },

    initChart(data, chart) {
      const xAxisName = data.map(item => item.ssrqid)
      const warehouseInWeight = data.map(item => item.spsli)
      const warehouseOutWeight = data.map(item => item.spslo)
      const warehouseOperation = data.map(item => item.spsl)
      let option = {
        title: {
          text: data.length > 0?data[0].aaa:"",
          left: "center",
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        legend: {
          top:"40"
        },
        xAxis: [{
          type: 'category',
          data: xAxisName,
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          name: '重量',
          axisLabel: {
            formatter: '{value}吨'
          },
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '入仓重量',
          type: 'bar',
          barGap:'20%',
          barCategoryGap: '40%',
          barWidth: '30%',
          data: warehouseInWeight,
        }, {
          name: '出仓重量',
          type: 'bar',
          barGap:'20%',
          barCategoryGap: '40%',
          barWidth: '30%',
          data: warehouseOutWeight,
        }, {
          name: '作业量',
          type: 'line',
          barWidth: '60%',
          data: warehouseOperation,
          markPoint: {
            data: [
              {type: 'max', name: '最大值'},
              {type: 'min', name: '最小值'}
            ]
          },
          markLine: {
            data: [
                {type: 'average', name: '平均值'}
            ]
          }
        }]
      }
      // 使用刚指定的配置项和数据显示图表。
      chart.setOption(option)
    },

    dateClick(index) {
      this.pageNum = index
      this.getList()
    },

    /**enter键触发 */
    enterClick(e) {
      this.getList()
      this.getChart()
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.reset()
      this.getWarehouse()
      this.getList()
      this.getChart()
    },
    reset() {
      Object.assign(this.$data, this.$options.data())
    },
  }
};
</script>
